import random
import threading
from threading import Thread
from time import sleep

list1 = [0] * 10

lock = threading.Lock()


def task1():
    # 获取线程锁 如果已经上锁 等待锁释放
    lock.acquire()  # 阻塞

    for i in range(len(list1)):
        list1[i] = 1
        sleep(0.5)
    lock.release()  # 释放


def task2():
    lock.acquire()
    for i in range(len(list1)):
        sleep(0.5)
        print(i)
    lock.release()


if __name__ == '__main__':
    t1 = Thread(target=task1)
    t2 = Thread(target=task2)

    t1.start()
    t2.start()
